from datetime import datetime
from django.contrib import admin
from django.utils.html import format_html

from .models import Banner

# Register your models here.
@admin.register(Banner)
class BannerAdmin(admin.ModelAdmin):
    list_display = ['pk', 'get_image', 'category', 'content', 'start_time', 'end_time', 'enable', 'order', 'remark', 'create_time']

    list_editable = ['enable', 'order']

    fieldsets = [
        (
            None,
            {
                "fields": ["image", "category", 'content', 'start_time', 'end_time', 'order'],
            },
        ),
        (
            "额外选项",
            {
                "classes": ["collapse"],
                "fields": ["remark", "enable"],
            },
        ),
    ]

    def get_image(self, obj):
        if obj.image:
            return format_html(
                '<img src="{}" width="100"/>',
                obj.image.url
            )
        return ''
